import Vue from "vue";
import VueRouter from "vue-router";
import Index from "../views/index.vue";
Vue.use(VueRouter);
const routes = [
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login.vue"),
  },
  {
    path: "/",
    name: "Index",
    component: Index,
    redirect: "/index",
    meta: { requiresAuth: true },
    children: [
      {
        path: "/index",
        name: "userinfo",
        redirect: "/userinfo/alluser",
        component: () => import("@/components/userinfo.vue"),
        children: [
          {
            path: "/userinfo/adduser",
            name: "adduser",
            component: () => import("@/components/userinfo/adduser.vue"),
          },
          {
            path: "/userinfo/alluser",
            name: "alluser",
            component: () => import("@/components/userinfo/alluser.vue"),
          },
          {
            path: "/userinfo/jsz",
            name: "jsz",
            component: () => import("@/components/userinfo/jsz.vue"),
          },
          {
            path: "/userinfo/slsc",
            name: "slsc",
            component: () => import("@/components/userinfo/slsc.vue"),
          },
          {
            path: "/userinfo/echarts",
            name: "echarts",
            component: () => import("@/components/userinfo/echarts.vue"),
          },
          {
            path: "/userinfo/newMoto",
            name: "newMoto",
            component: () => import("@/components/userinfo/newMoto.vue"),
          },
        ],
      },
      {
        path: "/userinfo/details",
        name: "details",
        component: () => import("@/components/userinfo/details.vue"),
      },
    ],
  },
  // {
  //   path: "/Mupload", // 手机端首页
  //   name: "Mupload",
  //   component: () => import("@/Moblie/Mupload.vue"),
  //   children: [
  //     {
  //       path: "/MIndex/Muser", // 手机端首页
  //       name: "Muser",
  //       component: () => import("@/Moblie/Muser.vue"),
  //     },
  //   ],
  // },
];

const router = new VueRouter({
  mode: "history",
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.matched.some((record) => record.meta.requiresAuth)) {
    //判断是否需要登
    if (localStorage.isLogin_) {
      next();
    } else {
      // console.log(to.path)
      next({
        path: "/login",
        query: { target: to.path },
      });
    }
  } else {
    next();
  }
});
export default router;
